package com.stylefeng.guns.modular.goods.controller;

import com.stylefeng.guns.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.goods.model.Safeguard;
import com.stylefeng.guns.modular.goods.service.ISafeguardService;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-09-05 16:09:26
 */
@Controller
@RequestMapping("/safeguard")
public class SafeguardController extends BaseController {

    private String PREFIX = "/goods/safeguard/";

    @Autowired
    private ISafeguardService safeguardService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "safeguard.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/safeguard_add")
    public String safeguardAdd() {
        return PREFIX + "safeguard_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/safeguard_update/{safeguardId}")
    public String safeguardUpdate(@PathVariable Long safeguardId, Model model) {
        Safeguard safeguard = safeguardService.selectById(safeguardId);
        model.addAttribute("item",safeguard);
        LogObjectHolder.me().set(safeguard);
        return PREFIX + "safeguard_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        return safeguardService.selectList(null);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Safeguard safeguard) {
        safeguardService.insert(safeguard);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long safeguardId) {
        safeguardService.deleteById(safeguardId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Safeguard safeguard) {
        safeguardService.updateById(safeguard);
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{safeguardId}")
    @ResponseBody
    public Object detail(@PathVariable("safeguardId") Long safeguardId) {
        return safeguardService.selectById(safeguardId);
    }
}
